package com.example.testproj.file;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.testproj.R;
import com.example.testproj.utils.ThreadUtil;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FileTestActivity extends AppCompatActivity {
    private static final String TAG = "FileTestActivity";
    private ExecutorService executorService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_test);

        executorService = Executors.newSingleThreadExecutor();

        executorService.submit(writeFile);
        executorService.submit(ReadFile);
    }

    private Runnable ReadFile=new Runnable() {
        @Override
        public void run() {
            File file= Environment.getExternalStorageDirectory();
            if (file.exists()){
                File testFile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.txt");
                if (testFile.exists()){
                    InputStream inputStream=null;
                    try {
                        inputStream=new FileInputStream(testFile);
                        BufferedInputStream bufferedInputStream=new BufferedInputStream(inputStream);
                        int b=0;
                        while ((b=bufferedInputStream.read())!=-1){
                            Log.d(TAG, "run: "+b);
                        }
                        Log.d(TAG, "run: 读完了");
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        if (inputStream!=null){
                            try {
                                inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
            }else {
                Log.d(TAG, "run: 没找着");
            }
        }
    };

    private Runnable writeFile=new Runnable() {
        @Override
        public void run() {
            File file= Environment.getExternalStorageDirectory();
            if (file.exists()){
                File testFile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.txt");
                if (testFile.exists()){

                }
                OutputStream outputStream=null;
                try {
                    outputStream=new FileOutputStream(testFile);
                    BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(outputStream);
                    for (int i=0;i<10;i++){
                        bufferedOutputStream.write(i);
                    }
                    bufferedOutputStream.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (outputStream!=null){
                        try {
                            outputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
                Log.d(TAG, "run: 写完了");
            }else {
                Log.d(TAG, "run: 没找着");
            }
        }
    };
}
